﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;


namespace Eshouse.Bll
{

    /// <summary>
    /// 生成 html 表格,主要是配合jquery_easy_ui的弹出框时嵌入html表格使用
    /// </summary>
    public class htmlUItableHelper
    {
      public enum htmlTap
        {
            txtBox = 1,
            txtArea = 2,
            txtPwd = 3,
            txtFile = 4,
            btn = 5,
            btnSbmit = 6,
            btnReset = 7,
            chkBox = 8,
            radio = 9,
            select = 10
        }
      public  enum tapState
        {
            none = 0,
            visibled = 1,
            hided = 2,
            disabled = 3,
            enabled = 4,
            check = 5,
            uncheck = 6
        }
        static string strSelOption = "";
        /// <summary>
        /// select html控件的数据源 sql 
        /// </summary>
        public static string SelOpton
        {
            set { strSelOption = value; }
            get { return strSelOption; }
        }

        /// <summary>
        /// 创建修改某条数据时弹出的Html表格，根据传入的datatable 创建一个可编辑的表单元素的html表格,对这个datatable有严格要求，
        /// 第一：datatable仅能有四行，第二行为对应的中文名，第1行为对应的数据,第三行为Html表单元素的对应枚举,第四行为html表单元素的状态,第五行为该表单元素的个数
        /// 第二：如果一个字段需要对用多个表单元素，请利用"|"分割；如果一个表单元素需要多种状态,请利用"|"分割
        /// 第三：数据行的第一列必须为该条数据的ID
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static string CreateEditHtmlTable(DataTable dt)
        {
            int colCount = dt.Columns.Count;
            DataRow chName = dt.Rows[1];
            DataRow datarow = dt.Rows[0];
            DataRow formTap = dt.Rows[2];
            DataRow tapState = dt.Rows[3];
            DataRow tapNum = dt.Rows[4];

            int trCount = ((colCount - 1 )/ 2);
            if ((colCount - 1) % 2 != 0)  //奇数
            {
                trCount++;
            }
            StringBuilder sbHtml = new StringBuilder();
            sbHtml.Append("<p id=\"editID\" style='display:none'>" + datarow[0].ToString() + "</p>");
            sbHtml.Append("<table class=\"formTable\"><caption>修改信息</caption><tbody>");
            int preTD = 0;
            int nextTD = 0;
            string tap1 = "";
            string tap2 = "";
            string val1 = "";
            string val2 = "";
            int num1 = 1;
            int num2 = 1;
            for (int i = 1; i <= trCount; i++)  ///创建行,创建列，每一行两列 ,第一列为 id 跳过
            {
                preTD = preTD + 1;
                nextTD = nextTD + preTD + 1;
                if (nextTD <= colCount)
                {
                    val1 = datarow[preTD].ToString();
                    val2 = datarow[nextTD].ToString();
                    num1 = tapNum[preTD].ToString() == "" ? 1 : Convert.ToInt32(tapNum[preTD]);
                    string [] stateList=tapState[preTD].ToString().Split('|');  //分割状态
                    for (int n = 0; n < num1;n++ )
                    {
                        tap1 = tap1 + CreateHtmlTap(Convert.ToInt32(formTap[preTD]), val1, stateList[n], dt.Columns[preTD].ColumnName + "_" + n.ToString(), dt.Columns[preTD].ColumnName);
                    }

                    num2 = tapNum[nextTD].ToString() == "" ? 1 : Convert.ToInt32(tapNum[nextTD]);

                    stateList = tapState[nextTD].ToString().Split('|');  //分割状态
                    for (int n = 0; n < num2; n++)
                    {
                        tap2 = tap2 + CreateHtmlTap(Convert.ToInt32(formTap[nextTD]), val2, stateList[n], dt.Columns[nextTD].ColumnName + "_" + n.ToString(), dt.Columns[nextTD].ColumnName);
                    }
      
                    sbHtml.Append(string.Format("<tr><td class=\"label\">" + chName[preTD].ToString() + "：</td><td style=\"width:230px\">{0}</td><td class=\"label\"  >" + chName[nextTD].ToString() + "：</td><td>{1}</td></tr>",tap1,tap2));
                }
                else
                {
                    val1 = datarow[preTD].ToString();
                    num1 = tapNum[preTD].ToString() == "" ? 1 : Convert.ToInt32(tapNum[preTD]);
                    string[] stateList = tapState[preTD].ToString().Split('|');  //分割状态
                    for (int n = 0; n < num1; n++)
                    {
                        tap1 = tap1 + CreateHtmlTap(Convert.ToInt32(formTap[preTD]), val1, stateList[n], dt.Columns[preTD].ColumnName + "_" + n.ToString(), dt.Columns[preTD].ColumnName);
                    }

                    sbHtml.Append(string.Format("<tr><td class=\"label\" colspan=\"2\">" + chName[preTD].ToString() + "：</td><td style=\"width:230px\">{0}</td></tr>",tap1));
                }
            }
            sbHtml.Append("</tbody></table><p id=\"errMsg\" style=\" color:red; padding-top:4px\"></p>");
            return sbHtml.ToString();
        }
        //tapState 可能会存在多种状态 
        private static string CreateHtmlTap(int tapName, string tapValue, string tapState, string tapID,string tapNameAtrr)
        {
            string strTap = "";
            string state = "";
            
            if (tapState.Contains("0"))
                state = "";

            if (tapState.Contains("1"))
                state = state + " visible=\"true\" ";

            if (tapState.Contains("2"))
                state = state + " visible=\"false\" ";

            if (tapState.Contains("3"))
                state = state + " disabled=\"disabled\" ";

            if (tapState.Contains("4"))
                state = state + " disabled=\"\" ";

            if (tapState.Contains("5"))
                state = state + " checked=\"checked\" ";

            if (tapState.Contains("6"))
                state = state + " checked=\"\" ";

            switch (tapName)
            {
                case 1:
                    strTap = string.Format("<input {0} id='{1}' type='text' value='{2}' />", state, tapID, tapValue);
                    break;
                case 2:
                    strTap = string.Format("<textarea {0} id='{1}' rows='5' cols='50' style='width:250px; height:60px' >{2}</textarea>", state, tapID, tapValue);
                    break;
                case 3:
                    strTap = string.Format("<input {0} id='{1}' type='password' value='{2}' />", state, tapID, tapValue);
                    break;
                case 4:
                    strTap = string.Format("<input {0} id='{1}' type='file' />", state, tapID);
                    break;
                case 5:
                    strTap = string.Format("<input  {0} id='{1}'  type='button'  value='提  交' />", state, tapID);
                    break;
                case 6:
                    strTap = string.Format("<input  {0} id='{1}'  type='submit'  value='提  交' />", state, tapID);
                    break;
                case 7:
                    strTap = string.Format("<input  {0} id='{1}'  type='reset'  value='重  置' />", state, tapID);
                    break;
                case 8:
                    strTap = string.Format("<input {0} id='{1}' type='checkbox' value='{2}'/>", state, tapID, tapValue);
                    break;
                case 9:
                    strTap = string.Format("<input {0} id='{1}' type='radio' value='{2}' name='{3}'  />", state, tapID, tapValue,tapNameAtrr);
                    break;
                case 10:/// select 控件 需要根据配置的sql读取 列表数据 然后形成select  
                    StringBuilder selTap = new StringBuilder();
                    selTap.Append("<select id='" + tapID + "' >");
                    DatabaseHelper dbhelper = new DatabaseHelper();
                    DataTable dt = dbhelper.ExecuteDataTable(strSelOption);
                    if (dt.Rows.Count == 0)
                    {
                        selTap.Append("<option value='0' selected='selected'>无数据</option>");
                    }
                    else
                    {
                        string itemTxt = "";
                        string itemVal = "";
                        for (int i = 0; i < dt.Rows.Count; i++)
                        {
                            itemVal = dt.Rows[i][0].ToString();
                            itemTxt = dt.Rows[i][1].ToString();
                            if (itemVal == tapValue)
                            {
                                selTap.Append(string.Format("<option value='{0}' selected='selected'>{1}</option>", itemVal, itemTxt));

                            }
                            else
                            {
                                selTap.Append(string.Format("<option value='{0}' >{1}</option>", itemVal, itemTxt));
                            }
                        }
                    }
                    selTap.Append("</select>");
                    strTap = selTap.ToString();
                    break;
            }
            return strTap;
        }
    }
}
